﻿@using Apps.Web.Core;
@using Apps.Common;
@using Apps.Models;
@using Apps.Models.Sys;
@using Apps.Locale;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index_Layout.cshtml";

    List<permModel> perm = null;
    //化产项目
}
<div style="position: absolute; width: 350px; min-height: 33px; padding-top: 3px;">
    <input id="txtQuery" type="text" class="searchText" style="width:200px" placeholder="可根据化产项目名称查询" />
    @Html.ToolButton("btnQuery", "fa fa-search", Resource.Query, ref perm, "Query", true)
</div>
<div class="mvctool" style="margin-left: 290px;">
    <input id="beginTime" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" type="text" class="searchText" data-options="required:true" style="width:100px" placeholder="报单开始时间" />
    <input id="endTime" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" type="text" class="searchText" data-options="required:true" style="width:100px" placeholder="报单结束时间" />
    <select id="formState" name="formState" style="width:130px" autocomplete="off">
        <option value="-2">审核状态</option>
        <option value="0">待审核</option>
        <option value="1">审核通过</option>
        <option value="-1">审核未通过</option>
    </select>
    @Html.ToolButton("btnQuery2", "fa fa-search", Resource.Query, ref perm, "Query", true)
    @Html.ToolButton("btnCreate", "fa fa-plus", Resource.Create, ref perm, "Create", true)
    @Html.ToolButton("btnEdit", "fa fa-pencil", Resource.Edit, ref perm, "Edit", true)
    @Html.ToolButton("btnExamine", "fa fa-check", "审核", ref perm, "Examine", true)
    @Html.ToolButton("btnDel", "fa fa-trash", Resource.Delete, ref perm, "Delete", true)
    @Html.ToolButton("btnExport", "fa fa-level-up", Resource.Export, ref perm, "Export", true)

</div>
<div id="modalwindow" class="easyui-window" style="width:800px; height:400px;" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
<table>
    <tr>
        <td style="vertical-align: top; width: 200px;padding-top:1px">
            <!--化产项-->
            <table id="itemList" class="easyui-panel" style="width:205px;padding:10px;overflow:auto"></table>
        </td>
        <td style="width:3px;">&nbsp;</td>
        <td style="vertical-align: top">
            <table id="List"></table>
        </td>
    </tr>
</table>
<div id="Pager"></div>
<input type="hidden" id="itemId" value="" />
@Html.Partial("~/Views/Shared/_Partial_AutoGrid.cshtml")
<script type="text/javascript">
    var selectIndex = -1;
    function getCols(itemId) {
        var columns = [];
        $.ajax({
            url: '/HC_FORM/GetFiledList?itemId=' + itemId,
            type: 'GET',
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data != null) {
                    if (data.fieldListVo != null && data.fieldListVo.length > 0) {
                        $.each(data.fieldListVo, function (i, item) {
                            columns.push({ 'field': item.ExtendFieldNAME.toString().toUpperCase(), 'title': item.ExtendFieldTITLE,'align':'center','width':90, formatter: valueFormat });
                        });
                    }
                }
            }
        });
        return columns;
    }
    function gridBind(itemId, columns) {
        $('#List').datagrid({
            url: '/HC_FORM/GetList?itemId=' + itemId ,
            width: SetGridWidthSub(220),
            methord: 'post',
            height: SetGridHeightSub(46),
            fitColumns: true,
            sortName: 'ADDTIME',
            sortOrder: 'desc',
            idField: 'ID',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: true,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            //rownumbers: true,
            selectOnCheck: false,
            checkOnSelect: false,
            checkbox: true,
            frozenColumns: [[]],
            onLoadSuccess: function (data) {
                $("#List").datagrid("unselectAll"); //全部不选中
            },
            columns: [columns],
            onClickRow: function (index, row) {
            }
        });
    }

    function valueFormat(val, row, index) {
        var formatValue = val;
        var fieldValues = row.fieldList;
        if (fieldValues != null && fieldValues != undefined && fieldValues.length>0) {
            for (var i = 0; i < fieldValues.length; i++) {
                if (this.field == fieldValues[i].ExtendFieldNAME) {
                    if (fieldValues[i].CONTROL_TYPE == 2) {
                        if (val > fieldValues[i].MAXVALUE) {
                            formatValue = '<span style="color:red; font-weight:bold;">↑' + val + '</span>';
                        } else if (val < fieldValues[i].MINVALUE) {
                            formatValue = '<span style="color:red; font-weight:bold;">↓' + val + '</span>';
                        }
                        else {
                            formatValue = '<span style="color:green; font-weight:bold;">' + val + '</span>';
                        }
                    }
                }
            }
        }
        //$.ajax({
        //    url: '/HC_FORM/GetFormFiledValue?formId=' + row.ID + '&field=' + this.field,
        //    type: 'GET',
        //    async: false,
        //    dataType: 'json',
        //    success: function (data) {
        //        if (data != null) {
        //            if (data.CONTROL_TYPE == 2) {
        //                if (val > data.MAXVALUE) {
        //                    formatValue = '<span style="color:red; font-weight:bold;">↑' + val + '</span>';
        //                } else if (val < data.MINVALUE) {
        //                    formatValue = '<span style="color:red; font-weight:bold;">↓' + val + '</span>';
        //                }
        //                else {
        //                    formatValue = '<span style="color:green; font-weight:bold;">' + val + '</span>';
        //                }
        //            }             }
        //    }
        //});
            return formatValue;
    }

    $(function () {
        var firstId;
        var colss;
        var cols = [];
        $('#itemList').datagrid({
            url: '@Url.Action("GetItemList")',
            methord: 'post',
            width: SetGridWidthSub(220),
            height: SetGridHeightSub(45),
            fitColumns: true,
            sortName: 'ADDTIME',
            sortOrder: 'DESC',
            idField: 'ID',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: false,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            rownumbers: true,//行号
            title: '化产项目列表',
            onLoadSuccess: function (data) {
                var rows = $('#itemList').datagrid('getRows');
                if (rows != null) {
                    firstId = rows[0]['ID'];
                    $("#itemId").val(firstId);
                    getCols(firstId);
                    colss = getCols(firstId);
                    //cols = [{ field: 'ID', title: 'ID', width: 80, checkbox: true },];
                    cols = [{
                        field: 'ID', title: '<div class="datagrid-header-check"><input type="checkbox"></div>', formatter: function (value, row) {
                            if (row.STATE != 1) {
                                return '<div style="" class="datagrid-cell-check"><input type="checkbox" name="ck" value=""></div>';
                            }
                            return '';
                        }
                    },];
                    if (colss.length > 0) {
                        $.each(colss, function (i, item) {
                            cols.push(item);
                        });
                    }
                    cols2 = [
                        { field: 'ADDTIME', title: '创建时间', width: 110, sortable: true},
                        {
                            field: 'STATE', title: '审核状态', width: 80,sortable:true, formatter: function (value, row, index) {
                                if (value == 0) {
                                    return '待审核';
                                } else if (value == 1) {
                                    return '<font color="green">审核通过</font>';
                                } else {
                                    return '<font color=\'red\'>审核未通过</font> <a href=\'#\' onclick=\'showReason("' + row.ID + '")\'>查看原因</a>';
                                }
                            }
                        },
                        { field: 'AUDITTIME', title: '审核时间', width: 80, sortable: true },
                    ];
                    cols.push.apply(cols, cols2);
                    gridBind(firstId, cols);
                }
            },
            columns: [[
                { field: 'ID', title: '', width: 20, hidden: true },
                { field: 'ITEM_TITLE', title: '化产项目', width: 80 },
            ]],
            onClickRow: function (index, data) {
                //$("#beginTime").val('');
                //$("#endTime").val('');
                //$("#formState").attr("value", -2);
                var row = $('#itemList').datagrid('getSelected');
                if (row != null) {
                    $("#itemId").val(row.ID);
                    curName = row.NAME;
                    curItemId = row.ID;
                    getCols(curItemId);
                    colss = getCols(curItemId);
                    //cols = [{ field: 'ID', title: 'ID', width: 80, checkbox: true }, ];
                    cols = [{
                        field: 'ID', title: '<div class="datagrid-header-check"><input type="checkbox"></div>', formatter: function (value, row) {
                            if (row.STATE != 1) {
                         return '<div style="" class="datagrid-cell-check"><input type="checkbox" name="ck" value=""></div>';
                     }
                     return ''; }
                 },];
                    if (colss.length > 0) {
                        $.each(colss, function (i, item) {
                            cols.push(item);
                        });
                    }
                    cols2 = [
                    { field: 'ADDTIME', title: '创建时间', width: 80, sortable: true },
                    //{ field: 'UPDATETIME', title: '修改时间', width: 80 },
                    {
                        field: 'STATE', title: '审核状态', width: 80, sortable: true, formatter: function (value, row, index) {
                            if (value == 0) {
                                return '待审核';
                            } else if (value == 1) {
                                return '<font color="green">审核通过</font>';
                            } else {
                                return '<font color=\'red\'>审核未通过</font> <a href=\'#\' onclick=\'showReason("' + row.ID + '")\'>查看原因</a>';
                            }
                        }
                    },
                    { field: 'AUDITTIME', title: '审核时间', sortable: true, width: 80 },
                    ];
                    cols.push.apply(cols, cols2);
                    gridBind(curItemId, cols);
                    $("#List").datagrid("unselectAll"); //全部不选中
                }
            }
        }).datagrid('getPager');
    });
</script>

@*operation*@
<script type="text/javascript">
    //ifram 返回
    function frameReturnByClose() {
        $("#modalwindow").window('close');
    }
    function frameReturnByReload(flag) {
        if (flag)
            $("#List").datagrid('load');
        else
            $("#List").datagrid('reload');
    }
    function frameReturnByMes(mes) {
        $.messageBox5s(Lang.Tip, mes);
    }
    function showReason(formId) {
        $.get('@Url.Action("GetExamineReason")?formId=' + formId, function (result) {
            $.messager.show({
                title: '审核说明', msg: result, showType: 'show'
            });
        });
    }

    $(function () {
        $("#btnCreate").click(function () {
            var row = $('#itemList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='auto' frameborder='0'' src='@Url.Action("Create")?itemId=" + row.ID + "'></iframe>");
                $("#modalwindow").window({ title: Lang.Create, width: 650, height: 500, iconCls: 'fa fa-plus' }).window('open');
            }
            else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });

        $("#btnEdit").click(function () {
            var formRow = $('#List').datagrid('getSelected');
            if (formRow != null)
            {
                if (formRow.STATE == 1) { $.messageBox5s(Lang.Tip, '该记录已审核通过，不能再修改'); return; }
                else {
                    $("#modalwindow").html("<iframe width='100%' height='99%'  frameborder='0' src='@Url.Action("Edit")?itemId=" + $("#itemId").val() + "&formId=" + formRow.ID + "'></iframe>");
                    $("#modalwindow").window({ title: '@Resource.Edit', width: 650, height: 500, iconCls: 'fa fa-pencil' }).window('open');
                }
            }
            else
            {
                $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords');
            }
        });

        $("#btnExamine").click(function () {
            var formRow = $('#List').datagrid('getChecked');
            if (formRow != null && formRow.length > 0) {
                if (formRow.length > 10) {
                    { $.messageBox5s(Lang.Tip, '一次最多审核10条记录'); }
                    return;
                }
                 var formIds = ""
                $.each(formRow, function (i, row) {
                     if (row.STATE !=1) {
                         if (formIds == "")
                         {
                             formIds = row.ID;
                        }
                        else {
                             formIds = formIds + "," + row.ID;
                        }
                    }
                });

                if (formIds == "") {
                    $.messageBox5s(Lang.Tip, '没有可审批记录');
                    return;
                }
                else {

                    $("#modalwindow").html("<iframe width='100%' height='99%'  frameborder='0' src='@Url.Action("Examine")?itemId=" + $("#itemId").val() + "&formIds=" + formIds + "'></iframe>");
                    $("#modalwindow").window({ title: '报单审核', width: 400, height: 280, iconCls: 'fa fa-pencil' }).window('open');
                }
            }
            else
                {$.messageBox5s(Lang.Tip, '请至少选择一条记录');}
        });

        $("#btnQuery").click(function () {
            var queryStr = $("#txtQuery").val();
            if (queryStr == null) {
                queryStr = "";
            }
            $("#itemList").datagrid("load", { queryStr: queryStr });
        });

        $("#btnQuery2").click(function () {
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();
            var formState = $("#formState").val();
            if (beginTime.length > 0 && endTime.length > 0) {
                var newbeginTime = new Date(beginTime);
                var newendTime = new Date(endTime);
                if (newbeginTime > newendTime) {
                    $.messageBox5s(Lang.Tip, '开始时间不能大于结束时间');
                    return;
                }
            }
            $("#List").datagrid("load", { beginTime: beginTime, endTime: endTime, formState: formState });
        });

        $("#btnDel").click(function () {
            var itemId = $("#itemId").val();
             var row = $('#List').datagrid('getSelected');
            if (row != null) {
                $.messager.confirm(index_lang_tip, '@Resource.YouWantToDeleteTheSelectedRecords', function (r) {
                    if (r) {
                        $.post("@Url.Action("Delete")?itemId=" + itemId+"&id=" + row.ID, function (data) {
                            if (data.type == 1)
                                $("#List").datagrid('load');
                            $.messageBox5s(Lang.Tip, data.message);
                        }, "json");
                    }
                });
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });

        $("#btnExport").click(function () {
            var itemId = $("#itemId").val();
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();
            var formState = $("#formState").val();
            $.post("@Url.Action("CheckExportData")?itemId=" + itemId + "&beginTime=" + beginTime + "&endtime=" + endTime + "&formState=" + formState, function (data) {
                if (data.type == 1)
                {
                    window.location = "@Url.Action("Export")?itemId=" + itemId + "&beginTime=" + beginTime + "&endtime=" + endTime + "&formState=" + formState;
                }
                else
                {
                $.messageBox5s(Lang.Tip, data.message);
                }
            }, "json");
        });
        $(window).resize(function () {
            $('#List').datagrid('resize', {

            }).datagrid('resize', {
                width: SetGridWidthSub(220),
                height: SetGridHeightSub(46)
            });

            $("#p").height(SetGridHeightSub(100));
        });
    });
</script>